﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Money
{
    // TODO consider an automatch that will do a first pass reconcile?

    public partial class TransferReconcileForm : Form
    {
        // TODO we may want to refactor this out so that the sub form does not know about main document item
        private MoneyDoc _doc = null;
        List<Transaction> _sourceTransactions = null;

        public TransferReconcileForm(MoneyDoc document)
        {
            _doc = document;

            InitializeComponent();

            _sourceTransactions = _doc.UnmatchedTransfers().ToList();

            _sourceGrid.AutoGenerateColumns = false;
            _destinationGrid.AutoGenerateColumns = false;

            _sourceGrid.DataSource = _sourceTransactions;
            _sourceGrid.Refresh();
        }

        private void _sourceGrid_SelectionChanged(object sender, EventArgs e)
        {
            if (_sourceGrid.SelectedRows.Count == 1)
            {
                Transaction sourceTransaction = _sourceGrid.SelectedRows[0].DataBoundItem as Transaction;

                if (sourceTransaction != null)
                {
                    List<Transaction> destinationTransactions = _doc.GetTransferMatch(sourceTransaction).ToList();
                    _destinationGrid.DataSource = null;
                    _destinationGrid.DataSource = destinationTransactions;
                    _destinationGrid.Refresh();

                    // select the first entry that we have not matched yet
                    foreach (DataGridViewRow row in _destinationGrid.Rows)
                    {
                        Transaction transaction = row.DataBoundItem as Transaction;

                        if (transaction != null)
                        {
                            if (transaction.DestinationTransactionNo == 0)
                            {
                                row.Selected = true;
                                break;
                            }
                            else
                            {
                                row.Selected = false;
                            }
                        }
                    }
                }
            }
        }

        private void _matchButton_Click(object sender, EventArgs e)
        {
            Transaction sourceTransaction = null;
            Transaction destinationTransaction = null;

            if (_sourceGrid.SelectedRows.Count == 1)
            {
                sourceTransaction = _sourceGrid.SelectedRows[0].DataBoundItem as Transaction;
            }
            if (_destinationGrid.SelectedRows.Count == 1)
            {
                destinationTransaction = _destinationGrid.SelectedRows[0].DataBoundItem as Transaction;
            }

            if (sourceTransaction != null && destinationTransaction != null)
            {
                _doc.ReconcileTransferTransactions(sourceTransaction, destinationTransaction,-1);
                _sourceGrid.Refresh();
                _destinationGrid.Refresh();
            }
        }

        private void _sourceGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView dataGrid = (DataGridView)sender;
            if (e.RowIndex >= 0)
            {
                Transaction transaction = dataGrid.Rows[e.RowIndex].DataBoundItem as Transaction;

                if (transaction.Type == Transaction.TransactionType.Transfer 
                    && (string.IsNullOrWhiteSpace(transaction.DestinationAccount) || transaction.DestinationTransactionNo == 0))
                {
                    e.CellStyle.BackColor = Color.LightCoral;
                    e.CellStyle.SelectionBackColor = Color.Coral;
                }
                else
                {
                    e.CellStyle.BackColor = Color.LightGreen;
                    e.CellStyle.SelectionBackColor = Color.Green;
                }
            }
        }

        private void _destinationGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView dataGrid = (DataGridView)sender;
            if (e.RowIndex >= 0)
            {
                Transaction transaction = dataGrid.Rows[e.RowIndex].DataBoundItem as Transaction;

                if (string.IsNullOrWhiteSpace(transaction.DestinationAccount) || transaction.DestinationTransactionNo == 0)
                {
                    e.CellStyle.BackColor = Color.LightCoral;
                    e.CellStyle.SelectionBackColor = Color.Coral;
                }
                else
                {
                    e.CellStyle.BackColor = Color.LightGreen;
                    e.CellStyle.SelectionBackColor = Color.Green;
                }
            }
        }

        private void _notTransferButton_Click(object sender, EventArgs e)
        {
            Transaction sourceTransaction;

            if (_sourceGrid.SelectedRows.Count == 1)
            {
                sourceTransaction = _sourceGrid.SelectedRows[0].DataBoundItem as Transaction;
                sourceTransaction.Type = Transaction.TransactionType.Unknown;
                _sourceGrid.Refresh();
            }
        }

        /// <summary>
        /// Remove any links that may have been made between transactions and reset the source transaction back to default
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _resetSourceTransaction_Click(object sender, EventArgs e)
        {
            Transaction sourceTransaction;

            if (_sourceGrid.SelectedRows.Count == 1)
            {
                sourceTransaction = _sourceGrid.SelectedRows[0].DataBoundItem as Transaction;

                _doc.ClearTransferLink(sourceTransaction);
                _doc.ResetTransaction(sourceTransaction);
                _sourceGrid.Refresh();
                _destinationGrid.Refresh();
            }
        }

        private void _removeSourceLink_Click(object sender, EventArgs e)
        {
            Transaction sourceTransaction;

            if (_sourceGrid.SelectedRows.Count == 1)
            {
                sourceTransaction = _sourceGrid.SelectedRows[0].DataBoundItem as Transaction;

                _doc.ClearTransferLink(sourceTransaction);
                _sourceGrid.Refresh();
                _destinationGrid.Refresh();
            }
        }

        private void _removeDestinationLinkButton_Click(object sender, EventArgs e)
        {
            Transaction sourceTransaction;

            if (_destinationGrid.SelectedRows.Count == 1)
            {
                sourceTransaction = _destinationGrid.SelectedRows[0].DataBoundItem as Transaction;

                _doc.ClearTransferLink(sourceTransaction);
                _sourceGrid.Refresh();
                _destinationGrid.Refresh();
            }
        }

        private void _autoReconcileButton_Click(object sender, EventArgs e)
        {
            _doc.AutoReconcileTransfers();
            _sourceGrid.Refresh();
            _destinationGrid.Refresh();
        }
    }
}
